#include "menutop.h"
#include "ui_menutop.h"
#include <QDebug>
#include <QPixmap>
#include <QPainter>
#include <QLabel>

// 画圆形图片
QPixmap pixmapToRound(QPixmap &src, int radius)
{
    if(src.isNull())
    {
        return QPixmap();
    }
    QPixmap pix(src.width(), src.height());
    pix.fill(Qt::transparent);  //设置图片透明
    QPainter painter(&pix);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    QPainterPath path;
    path.addEllipse(src.rect().center(), radius, radius);
    painter.setClipPath(path);

    painter.drawPixmap(QRect(src.width() / 2 - radius, src.height() / 2 - radius,
                             radius * 2, radius * 2), src);
    return pix;
}

MenuTop::MenuTop(QWidget *parent) : QWidget(parent), ui(new Ui::MenuTop)
{
    //ui->setupUi(this);
    //QRect screenRect = QApplication::desktop()->availableGeometry(QApplication::desktop()->primaryScreen());
    //this->setGeometry(0,0,screenRect.width(), screenRect.height());
    // 设置背景
    setStyleSheet("QWidget {background-color:rgb(237,243,247);}");
    // 创建控件
    layout = new QHBoxLayout();
    labelMain = new QLabel("主页");
    labelFresh = new QLabel();
    labelBlog = new QLabel("博客");
    labelUser = new QLabel("访客");
    labelLogin = new QLabel();
    labelMsg = new QLabel("消息");
    labelPublish = new QLabel("发布");
    labelRes = new QLabel("发资源");
    comboBox = new QComboBox();
    spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    // 加载控件
    layout->addWidget(labelMain);
    //layout->addWidget(labelBlog);
    layout->addWidget(comboBox);
    //layout->addWidget(labelFresh);
    layout->addItem(spacer);
    layout->addWidget(labelLogin);
    layout->addWidget(labelUser);
    layout->addWidget(labelMsg);
    layout->addWidget(labelPublish);
    comboBox->addItem("博客");
    comboBox->addItem("资源");
    comboBox->setMinimumWidth(70);
    connect(comboBox, SIGNAL(currentIndexChanged(int)), SLOT(comboChange(int)));
    // 设置margin
    layout->setContentsMargins(5, 0, 5, 0);
    // 加载布局
    setLayout(layout);
    // 找到图片
    QPixmap px(":/res/login.jpeg"); // 缩放
    QPixmap dst = px.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    // 加载图片到label
    labelLogin->setPixmap(dst);
    QPixmap px1(":/res/fresh.jpeg"); // 缩放
    QPixmap dst1 = px1.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst1 = pixmapToRound(dst1, 25);
    // 加载图片到label
    labelFresh->setPixmap(dst1);
    // QLabel加入到list
    labelList << labelMain << labelBlog << labelFresh<< labelLogin << labelUser << labelMsg << labelPublish;
    // 配置字体
    QFont font;
    // 加粗
    font.setBold(true);
    // 楷体
    font.setFamily("KaiTi");
    // 字号
    font.setPointSize(20);
    for(int i=0; i<labelList.length(); i++)
    {
        // 加载字体
        (labelList.at(i))->setFont(font);
        // 设置事件过滤器
        (labelList.at(i))->installEventFilter(this);
    }
}

// 事件过滤器，实现点击效果
bool MenuTop::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<labelList.length(); i++)
    {
        if(obj == labelList.at(i))
        {
            // 鼠标移动到控件上
            if(event->type() == QEvent::Enter)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
            }
            // 鼠标离开控件
            else if(event->type() == QEvent::Leave)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");
            }
            // 鼠标单击
            else if(event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                // 左键单击
                if(mouseEvent->button() == Qt::LeftButton)
                {
                    // 发射信号到上层
                    emit signalLabelClicked((enum LabelNnum)i);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    return QObject::eventFilter(obj, event);
}

// 修改头像
void MenuTop::setPixLogin(QPixmap *pix)
{
    QPixmap dst = (*pix).scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    labelLogin->setPixmap(dst);
}
// 修改用户名
void MenuTop::setUser(QString user)
{
    labelUser->setText(user);
}

void MenuTop::comboChange(int idx)
{
    emit signalTypeChanged(idx);
}

MenuTop::~MenuTop()
{
    delete ui;
}
